package nina.yiyou.salary.output

import nina.yiyou.salary.output.util.KeyPrefixPrepender

class ResultMapMaker {

  ResultMapMaker(List<List> inputList) {
    _inputList = inputList
  }

  Map<?, Map> make() {
    return _inputList
        .collect { updateHeaderKey((Map) it[1], (String) it[0]) }
        .inject { sum, val -> sum.collectEntries { k, v -> [k, v + (val[k] ?: [:])] } } as Map<?, Map>
  }

  private Map updateHeaderKey(Map<?, Map> lineMap, String prefix) {
    def prepend = { map -> new KeyPrefixPrepender(map, prefix).prepend() }
    return lineMap.collectEntries { id, line -> [id, prepend(line)] }
  }

  private final List<List> _inputList
}
